#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>
# 02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#		   fixed bugs.
# 07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#		   Made changes to account for the replacement of syslogd
#		   with syslog-ng
#
##################################################################
# case2: Test if messages of all levels are logged.
#	For each level, a separate configuration file is
#	created and that will be used as syslog.conf file.
##################################################################

# Number of levels.
export TST_TOTAL=8

. syslog-lib.sh || exit 1

syslog_case2()
{
	level_no=0
	levels="emerg alert crit err warning notice info debug"
	tst_resm TINFO "testing whether messages are logged into log file"

	for level in $levels
	do
		tst_resm TINFO "Doing level: $level..."

		case "$CONFIG_FILE" in
		/etc/syslog.conf)
			# Create the configuration file specific to this level
			echo "mail.$level	$MAILLOG" >> $CONFIG_FILE
			;;

		/etc/rsyslog.conf)
			# Create the configuration file specific to this level
			echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
			echo "mail.$level	$MAILLOG" >> $CONFIG_FILE
			;;

		/etc/syslog-ng/syslog-ng.conf)
			echo "source src{ internal(); unix-dgram(\"/dev/log\"); udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
			echo "filter f_syslog_$level { level($level) and facility(mail); };"  >> $CONFIG_FILE
			echo "destination syslog-$level { file(\"$MAILLOG\"); };"  >> $CONFIG_FILE
			echo "log { source(src); filter(f_syslog_$level); destination(syslog-$level); };"  >> $CONFIG_FILE;;
		esac

		restart_syslog_daemon

		# Grepping pattern has to be changed whenever the executable name
		# changes, ex: syslogtst executable.
		# This check is neccessary for syslog-ng because $MAILLOG is
		# only created after syslogtst
		if [ -e "$MAILLOG" ]; then
			oldvalue=`grep -c "syslogtst: mail $level test\." $MAILLOG`
		else
			oldvalue=0
		fi

		# syslogtst has to be called with additional level argument(0-7)
		if ! syslogtst 2 $level_no 2>/dev/null; then
			cleanup 1
		fi
		sleep 2

		# check if $MAILLOG script exists
		if [ ! -e "$MAILLOG" ]; then
			tst_resm TBROK "$MAILLOG no such log file"
			cleanup 1
		fi

		newvalue=`grep -c "syslogtst: mail $level test" $MAILLOG`
		diff=$(( $newvalue - $oldvalue ))
		if [ $diff -eq 0 ]; then
			tst_resm TFAIL "***** Level $level failed *****"
			status_flag=1
		elif [ $diff -ge 1 ]; then
			tst_resm TPASS "***** Level $level passed *****"
		fi
		# Increment the level_no for next level...
		: $(( level_no += 1 ))

		incr_tst_count
	done
}

tst_resm TINFO "Test if messages of all levels are logged."
tst_resm TINFO "For each level, a separate configuration file is"
tst_resm TINFO "created and that will be used as syslog.conf file."

setup
syslog_case2
cleanup ${status_flag:=0}
